package com.emode.action;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;






import java.util.Set;

import org.apache.log4j.Logger;
import org.json.JSONArray;
import org.json.JSONObject;

import com.emode.base.BaseAction;
import com.emode.entity.Bbssection;
import com.emode.entity.Bbsusers;
import com.emode.entity.SysGroup;
import com.emode.entity.SysPrivilege;
import com.emode.test.logTest;
import com.emode.util.CommonUtils;
import com.opensymphony.xwork2.ActionContext;
public class AdminAction extends BaseAction<Bbsusers>{
	 private static Logger logger = Logger.getLogger(AdminAction.class);  

	private static final long serialVersionUID = 1L;
	public String backAdmin(){
		return "adminindex";
		
	}
	//显示所有版块 
	public String sectionList(){
		list = sectionservice.findAll();
		ActionContext.getContext().getValueStack().push(list);
		return "sectionlist";
		
	}
	public String toAddsection(){
		//准备回显用户组数据
		List<Bbsusers> users = userservice.findAll();
		ActionContext.getContext().put("users",users);	
		return "addsection";
		
	}
	//添加
	public String addSection(){
		section.setBbsusers(model);
		sectionservice.save(section);;
		return "tosection";
	}
	//修改  准备数据
	public String toEditUI(){
		
		Bbssection s = sectionservice.getById(section.getSid());
		ActionContext.getContext().put("list", s);
		List<Bbsusers> users = userservice.findAll();
		ActionContext.getContext().put("users",users);	
		return "toedit";
		
	}
	// 保存
	public String update(){
		if("".equals(section.getSname())&&"".equals(section.getSprofile())){
			return null;
			
		}else{
		section.setBbsusers(model);
		sectionservice.update(section);
		//sectionservice.update2(section);
		return "tosection";
		}
	}
	

	//显示所有用户
	public String userList(){
		/*
		 * List<Bbsusers> users = userservice.findAll();
		ActionContext.getContext().put("users",users);
		*/
		return "userlist";
		
	}
	public void getAllUsers() throws Exception {
		 //定义列名
		List<Object> user = new ArrayList<Object>();
		if(model.getUid()!=null&&model.getUid()!=0){
			Bbsusers u = userservice.getById(model.getUid());
			Set<SysGroup> g = u.getGroup();
			
			Map<String,Object> data = new HashMap<String,Object>();
			data.put("uid", u.getUid());
			data.put("uname", u.getUname());
			data.put("uemail", u.getUemail());
			data.put("point", u.getUpoint());
			data.put("class", u.getUclass());
			data.put("regdate", u.getUregDate());
			//暂时支持一个用户只能有一个用户组
			for(SysGroup group:g){
				data.put("group", group.getGroupName());
				System.out.println("groupName:"+group.getGroupName());
			}
			JSONObject obj = new JSONObject(data);
			//user.add(data);
//			Map<Object, Object> info = new HashMap<Object, Object>();
//			info.put("userinfo", user);
//			JSONObject obj = new JSONObject(info);
		 //   JSONArray array = new JSONArray(obj);
			System.out.println(obj.toString());
			CommonUtils.returnJson(obj.toString());
		}else{
		List<Bbsusers> users = userservice.findAll();
		
		for (Bbsusers u : users) {
			Set<SysGroup> g = u.getGroup();
			Map<String,Object> data = new HashMap<String,Object>();
			data.put("uid", u.getUid());
			data.put("uname", u.getUname());
			data.put("uemail", u.getUemail());
			data.put("point", u.getUpoint());
			data.put("class", u.getUclass());
			data.put("regdate", u.getUregDate());
			data.put("status", u.getUstate());
			if(g.isEmpty()){
				data.put("group", "未加入");
			}else{
			for(SysGroup group:g){
				data.put("group", group.getGroupName());
				//System.out.println("groupName:"+group.getGroupName());
			}
			}
			user.add(data);
		}
		System.out.println("---->:user 长度："+user.size()+1);
		 Map<Object, Object> info = new HashMap<Object, Object>();
		 info.put("data", user);
		info.put("recordsTotal", user.size()+1);
		JSONObject obj = new JSONObject(info);
		System.out.println(obj.toString());
		CommonUtils.returnJson(obj.toString());
		}
	}
//	public String test(){
//		System.out.println(111);
//		return "userlist";
//	}
	//删除
	public String del(){
		System.out.println("---->uid:"+users.getUid());
		userservice.deleteByIds(users.getUid());;
		return "touserlist";
		
	}
	//显示所有的话题
	public String topicList(){
		int count = topicservice.getTopicCount();
		p.init(count);
		list =topicservice.allTopics(p);
		ActionContext.getContext().getValueStack().push(list);
		return "topiclist";
	}
	
	//非法用户关进小黑屋
	public String lockUser() throws Exception{
		Bbsusers user = userservice.getById(users.getUid());
		if(0 == user.getUstate()){
			Short state = 4;
			user.setUstate(state);
			userservice.update(user);
			msg = "false";
			CommonUtils.returnJson(msg);
		}
		else{
			Short state = 0;
			user.setUstate(state);
			userservice.update(user);
			msg = "true";
			CommonUtils.returnJson(msg);
		}
		
		System.out.println("lock");
		return null;
	}
	//******************************用户组模块*****************//
	//显示所有的用户组
	public String usergroup(){
		List<SysGroup> group = groupservice.findAll();
		ActionContext.getContext().put("group",group);
		return "grouplist";
	}
	//添加用户组
	public String addgroup(){
		groupservice.save(group);
		return usergroup();
	}
	
	//更新用户组
	public String updategroup(){
		groupservice.update(group);
		return usergroup();
	}
	
	// *********************权限模块*************************//
	
}
